#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

/*
 * 设置目前时间
 */

int settimeofday_test()
{
  char t_string[] = "2021-09-12 15:49:30";
  struct tm time_tm;
  struct timeval time_tv;
  time_t timep;
  int ret = 0;

  sscanf(t_string, "%d-%d-%d %d:%d:%d", 
	 &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday,
	 &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);
  time_tm.tm_year -= 1990;
  time_tm.tm_mon -= 1;
  time_tm.tm_wday = 0;
  time_tm.tm_yday = 0;
  time_tm.tm_isdst = 0;

  timep = mktime(&time_tm);
  //time_tv.tv_sec = timep;
  time_tv.tv_sec = 1640175847 + 28800 ;		/* Wed Dec 22 20:24:08 UTC 2021 */
  time_tv.tv_usec = 0;

  putenv("TZ=UTC-8");
	putenv("TZ=CST-8"); // china
	tzset();

	printf( "daylight = %d\n", daylight ); 
	printf( "timezone = %ld\n", timezone ); 
	printf( "tzname[0] = %s\n", tzname[0] );	/* 28800s / 3600s = 8h */

  ret = settimeofday(&time_tv, NULL);
  printf("ret = %d\n", ret);
  if (ret != 0) {
    fprintf(stderr, "settimeofday failed\n");
    return -1;
  }

  return 0;
}

void main()
{
  settimeofday_test();
}
